﻿using System;
using System.Data;
using System.Web.Routing;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb
{
    public class Global : System.Web.HttpApplication
    {
        public static string LangDefault = "";
        public static CookieClass Cookie = new CookieClass();
        public static string FileExtension = "";

        void RegisterRoutes(RouteCollection routes)
        {
            // default
            RouteTable.Routes.MapPageRoute("Default", "default.html", "~/Default.aspx");
            RouteTable.Routes.MapPageRoute("Contact", "lien-he.html", "~/Modules/Pages/Contacts.aspx");
            RouteTable.Routes.MapPageRoute("PageDetail", "pages/{subject}/{pageid}.html", "~/Modules/Pages/PageDetail.aspx");

            //news
            RouteTable.Routes.MapPageRoute("NewsDetail", "tin-tuc/{subject}/{GroupNewsId}/{NewsId}.html", "~/Modules/News/NewsDetail.aspx");
            RouteTable.Routes.MapPageRoute("NewsViews", "tin-tuc/{subject}/{GroupNewsId}.html", "~/Modules/News/NewsViews.aspx");
            RouteTable.Routes.MapPageRoute("NewsDefault", "tin-tuc.html", "~/Modules/News/NewsDefault.aspx");

            //products
            RouteTable.Routes.MapPageRoute("ProductsDetail", "san-pham/{groupId}/{tag}/{proId}.html", "~/Modules/Products/ProductsDetail.aspx"); // chi tiết sp
            RouteTable.Routes.MapPageRoute("ProductsDemo", "demo/{tag}/{proId}.html", "~/Modules/Products/SlideProducts.aspx"); // demo sp
            RouteTable.Routes.MapPageRoute("ProductsDownload", "san-pham/{groupId}/{proId}-{tag}.html#download", "~/Modules/Products/ProductsDetail.aspx"); // download sp
            RouteTable.Routes.MapPageRoute("ProductsViews", "san-pham/{subject}-{groupId}.html", "~/Modules/Products/ProductsViews.aspx");// đến trang nhóm sp
            RouteTable.Routes.MapPageRoute("ProductDefault", "san-pham.html", "~/Modules/Products/ProducstDefault.aspx");// đến trang chủ nhóm sp

            RouteTable.Routes.MapPageRoute("ShopCart1", "gio-hang/{proId}.html", "~/Modules/Products/Shopcart.aspx");// đến trang giỏ hàng
            RouteTable.Routes.MapPageRoute("ShopCart2", "gio-hang.html", "~/Modules/Products/Shopcart.aspx");// đến trang giỏ hàng
            RouteTable.Routes.MapPageRoute("MemberLogon", "logon.html", "~/Modules/Products/MemberLogon.aspx");// đến trang
            RouteTable.Routes.MapPageRoute("MemberRegister", "register.html", "~/Modules/Products/Register.aspx");// đến trang

            //Search
            RouteTable.Routes.MapPageRoute("Search", "tim-kiem/{groupId}/{key}.html", "~/Modules/Pages/Search.aspx");

            //Admin
            RouteTable.Routes.MapPageRoute("Admin", "Admin.aspx", "~/Admins/Default.aspx");
            RouteTable.Routes.MapPageRoute("Logon", "Logon.aspx", "~/Admins/Logon.aspx");
            RouteTable.Routes.MapPageRoute("Logout", "logout.aspx", "~/Modules/Pages/Logout.aspx");
            RouteTable.Routes.MapPageRoute("Error", "Error.aspx", "~/Modules/Pages/Error.aspx");
        }
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            LangDefault = LanguageService.Language_GetByDefault();

            Application["HomNay"] = 0;
            Application["HomQua"] = 0;
            Application["TuanNay"] = 0;
            Application["TuanTruoc"] = 0;
            Application["ThangNay"] = 0;
            Application["ThangTruoc"] = 0;
            Application["TatCa"] = 0;
            Application["visitors_online"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            if (Session["Lang"] == null)
            {
                Cookie.SetCookie("Lang", LangDefault);
                Session["Lang"] = LangDefault;
            }
            if (Session["LangAdm"] == null)
            {
                Cookie.SetCookie("LangAdm", LangDefault);
                Session["LangAdm"] = LangDefault;
            }
            Session["uId"] = "";
            Session["pay"] = "";
            Session["newcus"] = "";
            Session["TemplateId"] = "";
            Session["PageId"] = "";
            // cai dat bo dem so ng truy cap
            Session.Timeout = 150;
            Application.Lock();
            Application["visitors_online"] = Convert.ToInt32(Application["visitors_online"]) + 1;
            Application.UnLock();
            try
            {
                DataBindSQL mThongKe = new DataBindSQL();
                DataTable dtb = mThongKe.TableThongKe();
                if (dtb.Rows.Count > 0)
                {
                    Application["HomNay"] = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
                    Application["HomQua"] = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
                    Application["TuanNay"] = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
                    Application["TuanTruoc"] = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
                    Application["ThangNay"] = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
                    Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
                    Application["TatCa"] = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
                }
                dtb.Dispose();
                mThongKe = null;
            }
            catch { }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
             //Code that runs when an unhandled error occurs
            //Exception ex = HttpContext.Current.Server.GetLastError();
            //if (ex.InnerException != null)
            //{
            //    ex = ex.InnerException;
            //}
            //if (ex is HttpException)
            //{
            //    if (((HttpException)ex).GetHttpCode() == 404)
            //    {
            //        Response.Redirect("~/FileNotFound.htm");
            //    }
            //    else
            //    {
            //        Response.Redirect("~/PageError.htm");
            //    }
            //}
            //HttpContext.Current.Server.ClearError();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            if (Session["Lang"] == null)
            {
                Session["Lang"] = GetLang();
            }
            if (Session["LangAdm"] == null)
            {
                Session["LangAdm"] = GetLangAdm();
            }
            Application.Lock();
            Application["visitors_online"] = Convert.ToUInt32(Application["visitors_online"]) - 1;
            Application.UnLock();
        }
        public static string GetLang()
        {
            return StringClass.Check(Cookie.GetCookie("Lang")) ? Cookie.GetCookie("Lang") : LangDefault;

        }

        public static string GetLangAdm()
        {
            return StringClass.Check(Cookie.GetCookie("LangAdm")) ? Cookie.GetCookie("LangAdm") : LangDefault;
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}